package com.turman.plyopengl

import android.content.Context
import android.graphics.Point
import android.graphics.PointF
import android.opengl.GLSurfaceView
import android.util.AttributeSet
import android.view.MotionEvent
import kotlin.math.sqrt

class DrawGLSurfaceView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
    GLSurfaceView(context, attrs) {

    private var plyRenderer: DrawPlyRenderer

    private var down_x = -1f
    private var down_y = -1f
    private var down_time = 0L
    private var pinchStartDistance = 0.0f
    private var downStartPoint:PointF?=null

    init {
        setEGLContextClientVersion(2)
        plyRenderer = DrawPlyRenderer(context)
        setRenderer(plyRenderer)
    }


    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action and MotionEvent.ACTION_MASK) {
            MotionEvent.ACTION_DOWN -> {
                down_x = event.x
                down_y = event.y
                down_time = System.currentTimeMillis()
            }

            MotionEvent.ACTION_MOVE -> {
                if (event.pointerCount == 1) {
                    //单指绘制
                    plyRenderer.drawRect(
                        context.pxToDp(down_x),
                        context.pxToDp(down_y),
                        context.pxToDp(event.x),
                        context.pxToDp(event.y)
                    )
                } else if (event.pointerCount == 2) {
                    //双指移动
                    if (downStartPoint==null) {
                        downStartPoint = PointF()
                        getPinchCenterPoint(event, downStartPoint!!)
                        pinchStartDistance = getPinchDistance(event)
                        return true
                    }
                    val pt = PointF()
                    getPinchCenterPoint(event, pt)
                    val dx = pt.x - downStartPoint?.x!!
                    val dy = pt.y - downStartPoint?.y!!
                    if (sqrt(dx*dx+dy*dy)>1f) {
                        downStartPoint?.x = pt.x
                        downStartPoint?.y = pt.y
                        val currentDistance = getPinchDistance(event)
                        LogUtil.i("buobao","${pt.x}, ${pt.y}")
                        plyRenderer.translate(context.pxToDp(dx), context.pxToDp(dy),currentDistance-pinchStartDistance)
                        pinchStartDistance = currentDistance
                    }
                }
            }

            MotionEvent.ACTION_UP -> {
                downStartPoint = null
                down_x =-1f
                down_y=-1f
            }
        }
        return true
    }

    private fun getPinchDistance(event: MotionEvent): Float {
        val x = event.getX(0) - event.getX(1)
        val y = event.getY(0) - event.getY(1)
        return sqrt((x * x + y * y).toDouble()).toFloat()
    }

    private fun getPinchCenterPoint(event: MotionEvent, pt: PointF) {
        pt.x = (event.getX(0) + event.getX(1)) * 0.5f
        pt.y = (event.getY(0) + event.getY(1)) * 0.5f
    }
}

























